#include "qwordtool.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QImage>
#include <QPainter>
#include "..\SceneData\WordShapeNode.h"
QWordTool::QWordTool(QObject *parent)
	: QMyTool(parent),pos()
{
	temp=0;
	isEditing=false;
	tmp_idx=-1;
}

QWordTool::~QWordTool()
{

}

void QWordTool::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	QGraphicsItem * cur=Container->mouseGrabberItem();
	if(controller->GetNodeType( Container->ItemIndex(cur))==WordShape)
	{
		if(isEditing)
		{
			//isEditing=false;
			controller->receiveMessage(new NewNodeMessage(NewNodeArgs(wordShapNode,pos,temp->toPlainText(),Container->font())));
			Container->removeItem(temp);
			delete temp;
			temp=0;
			
			int i=controller->NumberOfObjects()-1;
			Container->AddPixmapItem(i);
			((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
			((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
			((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
		}
		isEditing=true;
		tmp_idx=Container->ItemIndex(cur);
		Container->RemoveItemLink(cur);
		Container->removeItem(cur);
		delete cur;
		pos=QPointF(controller->GetValueByIndex(tmp_idx,posx),controller->GetValueByIndex(tmp_idx,posy));
		Container->SetPen(((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->LinePenRef());
		Container->SetBrush(((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->FillBrushRef());
		Container->setFont(((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->Font());
		temp=new QGraphicsTextItem(((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->Text(),0,Container);
		temp->setDefaultTextColor( ((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->LinePenRef().color());
		temp->setFont( ((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->Font());
		temp->setPlainText(((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->Text());
		temp->setFlag(QGraphicsItem::ItemIsSelectable, true);
		temp->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
		temp->setTextInteractionFlags(Qt::TextEditorInteraction);
		temp->setSelected(true);
		temp->setFocus(Qt::FocusReason::MouseFocusReason);
		temp->setTransform(QTransform(1,0,0,1,pos.x(),pos.y()));

	}
}
void QWordTool::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
}
void QWordTool::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(isEditing)
	{
		if(temp!=(Container->mouseGrabberItem()))
		{
			isEditing=false;
			int i;
			if(tmp_idx!=-1)
			{
				i=tmp_idx;
				((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->SetText(temp->toPlainText());
				((WordShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
				tmp_idx=-1;
			}
			else
			{
				controller->receiveMessage(new NewNodeMessage(NewNodeArgs(wordShapNode,pos,temp->toPlainText(),Container->font())));
				i=controller->NumberOfObjects()-1;
			}
			


			if(controller->GetNodeType(-1)==MovieClip)
			{
				controller->receiveMessage(new SelectContainerMessage(i));
				i=controller->NumberOfObjects()-1;
				((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
				((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
				controller->receiveMessage(new SelectContainerMessage());
			}
			else
			{
				((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
				((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
			}
			i=controller->NumberOfObjects()-1;
			Container->AddPixmapItem(i);
			Container->removeItem(temp);
			delete temp;
			temp=0;
		}

	}
	else if(!(Container->mouseGrabberItem()))
	{
		isEditing=true;
		pos=mouseEvent->scenePos();
		temp=new QGraphicsTextItem(0,Container);
		temp->setFlag(QGraphicsItem::ItemIsSelectable, true);
		temp->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
		temp->setTextInteractionFlags(Qt::TextEditorInteraction);
		temp->setSelected(true);
		temp->setFocus(Qt::FocusReason::MouseFocusReason);
		temp->setTransform(QTransform(1,0,0,1,pos.x(),pos.y()));
	}
}
void QWordTool::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
}
void QWordTool::contextMenuEvent(QGraphicsSceneContextMenuEvent * mouseEvent)
{
}
void QWordTool::Finalize()
{
	if(isEditing)
	{
		if(temp!=(Container->mouseGrabberItem()))
		{
			isEditing=false;
			int i;
			if(tmp_idx!=-1)
			{
				i=tmp_idx;
				((WordShapeNode*)(controller->GetObjectByIndex(tmp_idx)))->SetText(temp->toPlainText());
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
				tmp_idx=-1;
			}
			else
			{
				controller->receiveMessage(new NewNodeMessage(NewNodeArgs(wordShapNode,pos,temp->toPlainText(),Container->font())));
				i=controller->NumberOfObjects()-1;
			}

			if(controller->GetNodeType(-1)==MovieClip)
			{
				controller->receiveMessage(new SelectContainerMessage(i));
				i=controller->NumberOfObjects()-1;
				((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
				((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
				controller->receiveMessage(new SelectContainerMessage());
			}
			else
			{
				((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
				((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
				((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
			}
			i=controller->NumberOfObjects()-1;
			Container->AddPixmapItem(i);
			Container->removeItem(temp);
			delete temp;
			temp=0;
		}

	}
}